<script setup lang="ts">
import Motion from "@/plugins/Motion.ts"
import {onMounted, onUnmounted, ref, nextTick} from "vue";
import {useStore} from "vuex";
import demo1 from "@/static/image/demo1.png"
import demo2 from "@/static/image/demo2.png"
import demo3 from "@/static/image/demo3.png"
import demo4 from "@/static/image/demo4.png"
import demo5 from "@/static/image/demo5.png"
import demo6 from "@/static/image/demo6.png"
// Import Swiper Vue.js components
import {Swiper, SwiperSlide} from 'swiper/vue';
// Import Swiper styles
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
import {Autoplay} from 'swiper/modules';

const modules = [Autoplay];


let options = ref({
  maxDis: 150,
  pointNumbers: 100
});

let Montion;

let showMenu = ref(true);
let isShowWindow = ref(false);
let store = useStore();
let swiperColltor = ref<Element | any>(null);
let swiperCartoon = ref<Element | any>(null);
let swiperTextIndex = ref<number>(0);
let swiperTextList = ref([
  {
    text: "荆州数字大屏",
    url: "http://jinzhou.wzhyt.cn/",
    src: demo1
  },
  {
    text: "转运系统",
    url: "http://dg.wzhyt.cn/",
    src: demo2
  },
  {
    text: "同学录",
    url: "http://class.wzhyt.cn/",
    src: demo3
  },
  {
    text: "新疆数字大屏",
    url: "http://xjelectricity.wzhyt.cn/",
    src: demo4
  },
  {
    text: "饲喂系统大屏",
    url: "http://jxj.wzhyt.cn/",
    src: demo5
  },
  {
    text: "心意商城",
    url: "http://xysjd.wzhyt.cn/",
    src: demo6
  },
])

const MotionInit = () => {
  Montion = new Motion('canvas', options.value);
  Montion.clear();
  nextTick(() => {
    Montion.draw();
  })
}

// const ScrollInit = () => {
//   if (isShowWindow.value) {
//     let height = window.innerHeight;
//     let Aheight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;;
//     // console.log(Aheight, '==', height, [swiperCartoon.value])
//     // console.log((Aheight + height) - swiperCartoon.value.offsetTop)
//   }
// }

const onSwiper = (swiper) => {
  // console.log(swiper);
  swiperColltor.value = swiper;
};
const openUrl = (url) => {
  window.open(url)
}
const onSlideChange = (e) => {
  swiperTextIndex.value = e.realIndex;
};

const onMouseover = (index: number) => {
  swiperTextIndex.value = index;
  swiperColltor.value.slideToLoop(index)
  // swiperColltor.value.activeIndex = index;
}



onUnmounted(() => {
  // 页面销毁事件
  window.removeEventListener("resize", MotionInit);
  Montion.clear();
  // window.removeEventListener("scroll", ScrollInit);
})

onMounted(() => {
  MotionInit();
  // 绘制
  window.addEventListener("resize", MotionInit);
  // window.addEventListener("scroll", ScrollInit);

  const ob = new IntersectionObserver((e: any) => {
    // 监听动画是否出现在窗口范围内 出现一点则隐藏导航栏
    // console.log(e[0])
    let isShow = !e[0].isIntersecting;
    if (isShow) {
      showMenu.value = true;
    } else {
      showMenu.value = false;
    }
  }, {
    threshold: 1
  })
  let canvasBox: Element | any = document.querySelector(".canvasBox");
  ob.observe(canvasBox);

  const obTwo = new IntersectionObserver((e: any) => {
    // 监听动画是否出现在窗口范围内 出现一点则隐藏导航栏
    let isShow = e[0].isIntersecting;
    isShowWindow.value = isShow;
  }, {
    threshold: 0
  })
  // let canvasBox: Element | any = document.querySelector(".canvasBox");
  obTwo.observe(swiperCartoon.value);
})
</script>

<template>
  <HeaderMenu activeIndex="0" v-show="showMenu"/>
  <div class="swiperStatic">
    <div class="title">
      <h1>前端小洲</h1>
      <h2>记录和分享美好生活</h2>
    </div>
  </div>
  <div class="swiperCartoon">
    <div class="swiperHeader">
      <h1>作品展示</h1>
      <p>各类上线作品，小程序以及WEB端应用</p>
    </div>
    <div class="swiperContent" ref="swiperCartoon">
      <div class="swiperLeft">
        <Swiper
            class="swiper"
            :space-between="0"
            :modules="modules"
            :autoplay="{ delay: 3000, pauseOnMouseEnter: true }"
            :loop="true"
            @swiper="onSwiper"
            @realIndexChange="onSlideChange"
        >
          <swiper-slide v-for="(item,index) in swiperTextList" :key="index">
            <img @click="openUrl(item.url)" style="width: 100%;height: auto;cursor:pointer;" v-lazy="item.src"
                 :alt="'点击预览作品：' + item.text">
          </swiper-slide>
        </Swiper>
      </div>
      <div class="swiperRight">
        <div class="swiperLi" v-for="(item,index) in swiperTextList" @mouseover="onMouseover(index)"
             :class="{active:swiperTextIndex==index}"
             :key="index">
          <div class="img">
            {{ item.text.substring(0,1) }}
          </div>
          <div class="text">{{ item.text }}</div>
        </div>
      </div>
    </div>
  </div>
  <div class="canvasBack">
    <div class="canvasBox">
      <canvas id="canvas"></canvas>
      <div class="blogTitle">前端小洲</div>
      <div class="renderDiv">
        <label for="pointNumbers">点数量</label>
        <input id="pointNumbers" v-model.number="options.pointNumbers" type="number" min="50" max="999" maxlength="3">
        <label for="maxDis">最大显示间距</label>
        <input id="maxDis" v-model.number="options.maxDis" type="number" min="50" max="999" maxlength="3">
        <button class="render" @click="MotionInit">渲染</button>
      </div>
    </div>
  </div>
  <div class="main">

  </div>
  <FotterMessage/>
</template>

<style lang="scss" scoped>
.swiperStatic {
  width: 100%;
  height: 500px;
  background: linear-gradient(#d6e4fd 30%, #fae9f9 80%, transparent 100%);
  position: relative;

  .title {
    position: absolute;
    top: 50%;
    left: 35%;
    transform: translate(-50%, -50%);

    h1 {
      font-size: 50px;
      font-weight: 500;
      color: #333;
    }

    h2 {
      font-size: 30px;
      font-weight: 400;
      color: #676767;
    }
  }
}

.swiperCartoon {
  width: 100%;
  padding: 30px 0;

  .swiperHeader {
    width: 800px;
    margin: 20px auto;
    text-align: center;

    h1 {
      font-size: 35px;
      font-weight: 500;
      color: #333;
    }

    p {
      font-size: 20px;
      font-weight: 400;
      color: #676767;
    }
  }

  .swiperContent {
    width: 900px;
    margin: 0 auto;
    display: flex;

    .swiperLeft {
      width: 650px;
      height: 100%;

      .swiper {
        width: 100%;
        height: 100%;

      }
    }

    .swiperRight {
      width: 200px;
      margin-left: 50px;
      height: 100%;

      .swiperLi {
        display: flex;
        align-items: center;
        padding: 10px 20px;
        border-radius: 3px;
        cursor: pointer;
        user-select: none;

        &.active {
          background-color: #e9edff;
        }

        &:hover {
          background-color: #f2f6ff;
        }

        .img {
          width: 30px;
          height: 30px;
          border-radius: 3px;
          background-color: #2baee9;
          display: flex;
          align-items: center;
          justify-content: center;
          color: #fff;
        }

        .text {
          font-size: 14px;
          font-weight: 500;
          color: #666;
          margin-left: 20px;
        }
      }
    }
  }
}

.canvasBack {
  width: 100%;
  height: 300vh;
}

.canvasBox {
  width: 100%;
  height: 100vh;
  position: sticky;
  top: 0;

  .blogTitle {
    position: absolute;
    left: 200px;
    top: 200px;
    font-size: 40px;
    font-weight: 300;
    color: #fff;
    user-select: none;
  }

  #canvas {
    width: 100%;
    height: 100%;
    background-color: #222;

  }

  .renderDiv {
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    width: 200px;
    background-color: rgba(255, 255, 255, .3);
    display: flex;
    flex-direction: column;
    border-radius: 3px;
    padding: 15px;

    label {
      font-size: 18px;
      padding: 5px 0;
      color: #fff;
    }

    input {
      font-size: 18px;
      color: #fff;
      border: none;
      background-color: rgba(255, 255, 255, .2);
      padding: 5px;
      outline: none;

    }

    .render {
      width: 100%;
      border: none;
      outline: none;
      background-color: #fff;
      color: #222;
      padding: 5px 25px;
      border-radius: 3px;
      cursor: pointer;
      margin-top: 10px;
      font-size: 14px;
    }
  }

}

.main {
  width: 100%;

}

</style>
